package cn.aishua.ui.myKaka;

import org.apache.http.HttpResponse;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import cn.aishua.R;
import cn.aishua.network.HttpRequestUtil;
import cn.aishua.ui.myKaka.task.DataExchangeAsyncTask;

public class MyBankCardDetailActivity
    extends Activity
{
    // page header widgets
    private ImageView ivLeftBtn;
    private ImageView ivRightBtn;
    private TextView tvTitile;

    // widget for set as exchange card
    private TextView tvSet;
    private Button btnSet;

    private TextView tvBankType;
    private TextView tvCardType;
    private TextView tvCardNum;
    private TextView tvDate;
    private TextView tvCredit;

    private boolean isExchange = false;
    private String cardNum;

    // these two item are for change exchange card
    public final static String EXTRA_DATA_STR_CARD_NUM = "EXTRA_DATA_STR_CARD_NUM";
    public final static String EXTRA_DATA_BOOL_IS_SET = "EXTRA_DATA_BOOL_IS_SET";

    // this is for remove card
    public final static String EXTRA_DATA_STR_REMOVED_CARD_NUM = "EXTRA_DATA_STR_REMOVED_CARD_NUM";

    @Override
    protected void onCreate( Bundle savedInstanceState )
    {
        Log.d( "MyBankCardDetailActivity", "onCreate()" );
        super.onCreate( savedInstanceState );
        setContentView( R.layout.my_kaka_my_bank_card_detail );
        initPageHeader();
        initiate();

    }

    private void initPageHeader()
    {
        // left button
        ivLeftBtn = (ImageView) findViewById( R.id.page_header_iv_left_btn );
        ivLeftBtn.setImageResource( R.drawable.btn_back );
        ivLeftBtn.setOnClickListener( new View.OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                onBackPressed();
            }
        } );

        // title
        tvTitile = (TextView) findViewById( R.id.page_header_tv_title );
        tvTitile.setText( "我的银行卡" );

        // right button
        ivRightBtn = (ImageView) findViewById( R.id.page_header_iv_right_btn );
        ivRightBtn.setImageResource( R.drawable.btn_gar );
        ivRightBtn.setOnClickListener( new View.OnClickListener()
        {

            @Override
            public void onClick( View v )
            {
                remove();
            }
        } );
    }

    private void initiate()
    {

        tvSet = (TextView) findViewById( R.id.my_kaka_my_bank_card_detail_tv_set );
        btnSet = (Button) findViewById( R.id.my_kaka_my_bank_card_detail_btn_set );
        tvBankType = (TextView) findViewById( R.id.my_kaka_my_bank_card_detail_bank_type );
        tvCardType = (TextView) findViewById( R.id.my_kaka_my_bank_card_detail_card_type );
        tvCardNum = (TextView) findViewById( R.id.my_kaka_my_bank_card_detail_card_num );
        tvDate = (TextView) findViewById( R.id.my_kaka_my_bank_card_detail_date );
        tvCredit = (TextView) findViewById( R.id.my_kaka_my_bank_card_detail_credit );

        // load content based on whether this card is exchange card
        isExchange = getIntent().getBooleanExtra( MyBankCardActivity.EXTRA_DATA_BOOL_IS_EXCHANGE_CARD, false );
        String bankType = getIntent().getStringExtra( MyBankCardActivity.EXTRA_DATA_STR_BANK_TYPE );
        String cardType = getIntent().getStringExtra( MyBankCardActivity.EXTRA_DATA_STR_CARD_TYPE );
        cardNum = getIntent().getStringExtra( MyBankCardActivity.EXTRA_DATA_STR_CARD_NUM );
        String date = getIntent().getStringExtra( MyBankCardActivity.EXTRA_DATA_STR_DATE );
        String credit = getIntent().getStringExtra( MyBankCardActivity.EXTRA_DATA_STR_CREDIT );

        tvBankType.setText( bankType );
        tvCardType.setText( cardType );
        //        tvCardNum.setText( FormatConverter.toCardNumFormat( cardNum ) );
        tvCardNum.setText( cardNum );
        tvDate.setText( date );
        tvCredit.setText( credit );

        loadDynamicPart();

        if( "信用卡".equals( cardType ) )
        {
            btnSet.setVisibility( View.INVISIBLE );
        }

    }

    private void loadDynamicPart()
    {
        if( isExchange )
        {
            tvSet.setText( "已设置为兑现银行卡" );
            btnSet.setText( "取消设置" );
        }
        else
        {
            tvSet.setText( "否" );
            btnSet.setText( "设置兑现银行卡" );
        }
    }

    public void remove()
    {
        new AlertDialog.Builder( this ).setTitle( "删除该银行卡？" ).setMessage( null )
            .setPositiveButton( "是", new OnClickListener()
            {
                public void onClick( DialogInterface dialog, int which )
                {
                    int cardId = getIntent().getIntExtra( MyBankCardActivity.EXTRA_DATA_INT_CARD_ID, -1 );
                    new DataExchangeAsyncTask( MyBankCardDetailActivity.this, "删除银行卡" )
                    {

                        @Override
                        protected HttpResponse sendRequest( String... params )
                        {
                            return HttpRequestUtil.getInstance().removeBankCard( params );
                        }

                        @Override
                        protected void handleSuccResponse( String strJson ) throws Exception
                        {
                            Toast.makeText( MyBankCardDetailActivity.this, "银行卡已删除", Toast.LENGTH_SHORT ).show();
                            onBackPressed();
                            //                            Intent toMyBankCardPage = new Intent( MyBankCardDetailActivity.this,
                            //                                MyBankCardActivity.class );
                            //                            Log.d( EXTRA_DATA_STR_REMOVED_CARD_NUM, cardNum );
                            //                            toMyBankCardPage.putExtra( EXTRA_DATA_STR_REMOVED_CARD_NUM, cardNum );
                            //
                            //                            startActivity( toMyBankCardPage );
                        }
                    }.execute( "" + cardId );
                }
            } ).setNegativeButton( "否", null ).show();
    }

    public void setAsExchange( View view )
    {
        String title = null;
        if( isExchange )
        {
            title = "取消兑现银行卡设置？";
        }
        else
        {
            title = "设置为兑现银行卡？";
        }
        new AlertDialog.Builder( this ).setTitle( title ).setMessage( null )
            .setPositiveButton( "是", new OnClickListener()
            {

                public void onClick( DialogInterface dialog, int which )
                {
                    int cardId = getIntent().getIntExtra( MyBankCardActivity.EXTRA_DATA_INT_CARD_ID, -1 );

                    new DataExchangeAsyncTask( MyBankCardDetailActivity.this, "设置/取消兑现银行卡" )
                    {

                        @Override
                        protected HttpResponse sendRequest( String... params )
                        {
                            return HttpRequestUtil.getInstance().setCashingCard( params );
                        }

                        @Override
                        protected void handleSuccResponse( String strJson ) throws Exception
                        {
                            // stay in this page
                            isExchange = !isExchange;
                            loadDynamicPart();

                        }
                    }.execute( isExchange ? "cancel" : "enter", "" + cardId );

                }
            } ).setNegativeButton( "否", null ).show();
    }
}
